package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Yearcheckrecord;
import com.woniuxy.caradmin.entity.vo.YearcheckrecordVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.TroublerecordService;
import com.woniuxy.caradmin.service.YearcheckrecordService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import io.swagger.models.auth.In;
import lombok.Data;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/yearcheckrecord")
public class YearcheckrecordController {
    @Autowired
    private YearcheckrecordService yearcheckrecordService;
    @GetMapping("/yearcheckrecordById")
    @ApiOperation(value = "id查询年检记录")
    public R yearcheckrecordById(Integer id) {

        Yearcheckrecord yearcheckrecord = yearcheckrecordService.selectYearcheckrecordByid(id);
        if (yearcheckrecord !=null) {
            Date time  = yearcheckrecord.getTime();
            Date nexttime  = yearcheckrecord.getNexttime();
            java.sql.Date timesql=new java.sql.Date(time.getTime());
            java.sql.Date nexttimesql=new java.sql.Date(nexttime.getTime());
            yearcheckrecord.setTime(timesql);
            yearcheckrecord.setNexttime(nexttimesql);
            return R.ok().data("yearcheckrecord",yearcheckrecord);
        } else {
            throw new CarException(ResultCode.YEARISNULL, Constaint.YEARISNULL);
        }
    }

    @GetMapping("/yearcheckrecordList")
    @ApiOperation(value = "查询年检记录")
    public DataGridView yearcheckrecordList(YearcheckrecordVo yearcheckrecordVo) {
        Page<Object> page = PageHelper.startPage(yearcheckrecordVo.getPage(), yearcheckrecordVo.getLimit());
        List<Yearcheckrecord> yearcheckrecords = yearcheckrecordService.selectAllByPage(yearcheckrecordVo);
        if (yearcheckrecords != null && yearcheckrecords.size() > 0) {
            return new DataGridView(page.getTotal(), yearcheckrecords);
        } else {
            throw new CarException(ResultCode.YEARISNULL, Constaint.YEARISNULL);
        }
    }

    @GetMapping("/addyearcheckrecord")
    @ApiOperation(value = "提交年检记录")
    public R addyearcheckrecord(Yearcheckrecord yearcheckrecord, HttpServletRequest httpServletRequest) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
        String vid = httpServletRequest.getParameter("vid");
        String checknum = httpServletRequest.getParameter("checknum");
        String time = httpServletRequest.getParameter("time");
        String money = httpServletRequest.getParameter("money");
        String unitid = httpServletRequest.getParameter("unitid");
        String nexttime = httpServletRequest.getParameter("nexttime");
        String driverid = httpServletRequest.getParameter("driverid");
        String information = httpServletRequest.getParameter("information");
        yearcheckrecord.setVid(Integer.parseInt(vid));
        yearcheckrecord.setChecknum(checknum);
        java.sql.Date et = null;
        java.sql.Date st = null;
        try {
           Date  st1 = sdf.parse(time);
              st = new java.sql.Date(st1.getTime());
            Date et1 = sdf.parse(nexttime);
            et = new java.sql.Date(et1.getTime());
        } catch (ParseException e) {
            yearcheckrecord.setTime(st);
            yearcheckrecord.setNexttime(et);
        }

        yearcheckrecord.setMoney(Double.parseDouble(money));
        yearcheckrecord.setUnitid(Integer.parseInt(unitid));
        yearcheckrecord.setDriverid(Integer.parseInt(driverid));
        yearcheckrecord.setInformation(information);
        yearcheckrecord.setStatus(1);
        int i = yearcheckrecordService.addYearcheckrecord(yearcheckrecord);
        if (i > 0) {
            return R.ok().code(ResultCode.ADDYEARSUC).message(Constaint.ADDYEARSUC);
        } else {
            throw new CarException(ResultCode.ADDYEARFAIL, Constaint.ADDYEARFAIL);

        }
    }

    @GetMapping("/upyearcheckrecord")
    @ApiOperation(value = "修正年检记录")
    public R upyearcheckrecord(Yearcheckrecord yearcheckrecord) {

        int i = yearcheckrecordService.upYearcheckrecord(yearcheckrecord);
        if (i > 0) {
            return R.ok().code(ResultCode.UPYEARSUC).message(Constaint.UPYEARSUC);
        } else {
            throw new CarException(ResultCode.UPYEARFAIL, Constaint.UPYEARFAIL);
        }
    }

    @GetMapping("/removeyearcheckrecord")
    @ApiOperation(value = "删除年检记录")
    public R removeyearcheckrecord(Integer[] ids) {

        boolean b = yearcheckrecordService.removeYearcheckrecord(ids);
        if (b) {
            return R.ok().code(ResultCode.REMOVEYEARSUC).message(Constaint.REMOVEYEARSUC);
        } else {
            throw new CarException(ResultCode.REMOVEYEARFAIL, Constaint.REMOVEYEARFAIL);
        }
    }
}

